package com.project.forlearningdemo.config;

import com.project.forlearningdemo.utils.TokenUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader(TokenUtil.tokenHead);
        if(null==token||"".equals(token)){
            System.out.println("无token");
            response.sendRedirect("http://localhost:8088/forlearning/user/uauth");
            return false;
        }
        try {
            TokenUtil.getTokenBody(token);
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("http://localhost:8088/forlearning/user/uauth");
            return false;
        }
        return true;
    }

}
